Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

217
Visualizações
The difference between "" vs " " Javascript

I used this to remove white spaces from a string.

returnString.split(" ").filter(substr => substr !== "");

In my head it should be this:

returnString.split(" ").filter(substr => substr !== " ")    //note the space between the " "

why doesnt the bottom one work? Is it JS sytax?

Answer: If theres a space at the beginning of the string, its split with an empty string, (so substr !== "") removes that from the returned array when splitting the string.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

returnString.replace(/\s/g,'') Would be a superior way of doing this.

"" is an empty string.

" " is a space character.

They cannot be used interchangeably.

about 4 years ago · Juan Pablo Isaza Relatório

0

"" is an empty string.

If there is a space at the beginning of the string, it will split that space with an empty string.

that is why substr !== "" is needed to remove the empty string.

about 4 years ago · Juan Pablo Isaza Relatório

0

As mentioned in the comments, you’re splitting on whitespaces, creating indices for said whitespaces in an array.

const returnString = 'Hello I am Victor'

const str = returnString.split("").filter(substr => substr !== " ");

console.log(str.join(''))

would get you the desired result.

However, a regex pattern would be best suited for this use case.

returnString.replace(/\s/g, '')
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda